#include "HomePage.h"

HomePage::HomePage(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建搜索框
    searchBox = new QLineEdit(this);
    searchBox->setPlaceholderText("搜索");
    searchBox->setFixedHeight(40);

    QString searchStyle = R"(
        QLineEdit {
            padding-left: 10px;
            border: 1px solid #ccc;
            border-radius: 10px;
            font-size: 14px;
        }
        QLineEdit:focus {
            border: 1px solid #4A90E2;
        }
    )";
    searchBox->setStyleSheet(searchStyle);

    mainLayout->addWidget(searchBox); // 搜索框位于最顶部

    BlogListWidget *blogListWidget = new BlogListWidget(this);

    mainLayout->addWidget(blogListWidget);

    setLayout(mainLayout);

    // 设置按钮激活样式
    QString activeStyle = R"(
        QPushButton {
            background-color: #4A90E2;
            border: 1px solid #357AE8;
            color: white;
            border-radius: 5px;
            font-size: 14px;
        }
    )";

    // 连接搜索框回车信号和搜索函数
    connect(searchBox, &QLineEdit::returnPressed, [=]()
            {
                QString keyword = searchBox->text();
                this->searchBlogs(keyword); // 假设 BlogListWidget 有提供搜索接口
            });
    connect(blogListWidget, &BlogListWidget::openDetailPage, this, &HomePage::openDetailPage);
}

void HomePage::searchBlogs(const QString &keyword)
{
    // 清除现有内容
    // 根据 keyword 进行过滤或网络请求
    // 然后重新加载匹配的博客数据
    qDebug() << "Searching for blogs with keyword: " << keyword;
}
